from fastapi import APIRouter
from src.utils.common_logger import logger
from fastapi.requests import Request
from fastapi.responses import JSONResponse
import psutil
import platform

router = APIRouter()


@router.get("/index")
def test():
    return "ok"


@router.get("/logger_demo")
def logger_demo(request: Request):
    logger.info("来了老弟？")
    try:
        1 / 0
    except Exception as e:
        logger.error(f"算错了{str(e)}")

    return "info"


from src.utils.common_exception import AuthException


@router.get("/excepion_demo")
def excepion_demo(request: Request):
    raise AuthException(msg="你没有认证通过")
    return 'excepion_demo'


@router.get("/cpu")
async def info(request: Request):
    mem = psutil.virtual_memory()
    cpu_total = f"物理核心数: {psutil.cpu_count(logical=False)}"
    cpu_size = f"cpu总使用率: {psutil.cpu_percent(interval=1)}%"
    mem_total = f"可用内存: {mem.available / (1024 ** 3):.2f}GB"
    mem_size = f"内存使用率: {mem.percent}%"
    system = platform.system()
    banbenhao = platform.release()
    return JSONResponse(
        {'cpu_total': cpu_total, 'cpu_size': cpu_size, 'mem_total': mem_total, 'mem_size': mem_size, 'system': system,
         'banbenhao': banbenhao})
